package com.theo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.theo.entity.MaskInfo;
import com.theo.entity.TripInfo;
import com.theo.service.TripInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Controller
@RequestMapping("/trip-info")
public class TripInfoController {
    @Autowired
    private TripInfoService tripInfoService;

    @RequestMapping("/input.do")
    public String saveTripInfo(TripInfo tripInfo) {
        tripInfoService.saveTripInfo(tripInfo);
        return "redirect:/trip-info/show.do";
    }

    @RequestMapping("/show.do")
    public String showTripInfo(Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<TripInfo> list = tripInfoService.queryAllTripInfo();
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "admin/trip_list";
    }

    @RequestMapping("/show")
    public String show(@RequestParam(value = "type",defaultValue = "0") Integer type, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        List<TripInfo> list = null;
        if (type != 0) {
            list = tripInfoService.queryAllByTripType(type);
        } else {
            list = tripInfoService.queryAllTripInfo();
        }
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "user/trip_list";
    }

    @RequestMapping("/showLimit")
    public String showLimit(@RequestParam("date") String date, @RequestParam("vehicleNum") String vehicleNum, @RequestParam("place") String place,Model model,@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 5);
        Map<String, Object> map = new HashMap<>();
        map.put("date", date);
        map.put("vehicleNum", vehicleNum);
        map.put("place", place);
        List<TripInfo> list = tripInfoService.queryAllByLimit(map);
        PageInfo pageInfo = new PageInfo(list, 8);
        model.addAttribute("pageInfo", pageInfo);
        return "user/trip_list";
    }
}

